<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;

abstract class CommonController extends Controller {

    protected $getCol = ['*'];

    public function upload(Request $request){
        $file = $request->file('file');
        $count = $request->input('count',0);
        if(empty($file) || !$file->isValid()){
            return responseFail('文件无效！');
        }
        $size =  $file->getSize();
        $ext = $file->getClientOriginalExtension();
        $module = $request->input('module');

        $this->validate($request, [
            'module'=>'required'
        ],[],['module'=>'module 模块名']);

        //检查目录
        $middlePath = "/$module/".date('Y-m-d');
        $absDir = Storage::disk('upload')->path($middlePath);
        if(!is_dir($absDir)){mkdir($absDir,0777,true);}

        //重命名
        $fileName = time().uniqid().".$ext";

        //如果是图片
        if(in_array($ext, ['jpeg','jpg','bmp','png'])){

            if($request->has('w')){
                $w = $request->input('w');
                $h = $request->input('h');
                if(!empty($w) && !empty($h)){
                    $this->validate($request, [
                        'file' => 'mimes:jpeg,bmp,png,gif|dimensions:width='.$w.',height='.$h,
                    ],[],['w'=>'宽', 'h'=>'高',]);
                }
            }

            $imgIsCut = $request->input('imgIsCut',env('IMG_IS_CUT',false));//是否需要压缩
            $imgIsCutSize = $request->input('imgIsCutSize',env('IMG_IS_CUT_SIZE',1024*1024*1));//压缩阈值
            $imgCutRatio = $request->input('imgCutRatio',env('IMG_CUT_RATION',700));//压缩比

            //如果需要压缩
            if($imgIsCut && $size>=$imgIsCutSize){
                $fileName = time().uniqid().'.jpg';
                $rs = Image::make($file->getRealPath())->resize($imgCutRatio, $imgCutRatio, function ($constraint) {
                    $constraint->aspectRatio();
                })->save($absDir."/$fileName");
                if($rs === false){
                    return responseFail('图片压缩失败');
                }
                $filePath = "$middlePath/$fileName";
            }else{
                $filePath = $file->storeAs($middlePath,$fileName,'upload');
            }
        }
        //如果是文件
        else{
            $filePath = $file->storeAs($middlePath,$fileName,'upload');
        }

        $filePath = '/storage/upload/'.$filePath;
        $filePath = Str::replace('//', '/', $filePath);
        $url = asset($filePath);

        return responseSuccess(compact('filePath','count','size','url'));
    }

    protected function handleChildren(&$records) {
        foreach ($records as $record) {
            if(count($record['children'])<=0){
                unset($record['children']);
            }else{
                $this->handleChildren($record['children']);
            }
        }
    }


}
